package com.sinosoft.web;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.sinosoft.domain.UserDTO;
import com.sinosoft.service.IServer;
import com.sinosoft.service.RequestCacheServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author loujiang
 * @date 2020/12/23 2:33 下午
 * @NOTE:
 */

@RestController
public class TestController {

    @Autowired
    IServer server;

    @Autowired
    private RequestCacheServer requestCacheServer;

    @RequestMapping("/test/error")
    public String testError() {
        return server.error();
    }


    @RequestMapping("timeout")
    public String timeOut(@RequestParam("time") int timeout) {
        return server.Retry(timeout);
    }

    @RequestMapping("getuser")
    public UserDTO timeOut(@RequestParam("name") String name) {
        //初始化一个上下文  在同一个上下文中只调用一次
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        UserDTO userDTO = requestCacheServer.requestCache(name);
        name = name + "123";
        userDTO = requestCacheServer.requestCache(name);
        //关闭上下文
        context.close();
        return userDTO;
    }
}
